﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.Json.Serialization;

namespace Xc.DataAccess.OneNetCore.Services
{
    /// <summary>
    /// 请求核心内容
    /// </summary>
    public abstract class RequestCore
    {
        /// <summary>
        /// 要请求的内容路径
        /// </summary>
        [JsonIgnore]
        internal protected abstract string RequestPath { get; }
        /// <summary>
        /// 内容类型
        /// </summary>
        [JsonIgnore]
        internal protected virtual string ContentType { get; } = "application/json";
        /// <summary>
        /// Http请求方法
        /// </summary>
        [JsonIgnore]
        internal protected virtual HttpMethod HttpMethod { get; } = HttpMethod.Post;
        /// <summary>
        /// 是否将Body发送出去
        /// </summary>
        [JsonIgnore]
        internal protected virtual bool SendBody { get;  } = true;
        public  string QueryParameter()
        {
            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            var properties=   this.GetType().GetProperties();
            foreach (var key in properties)
            {
                var attr= (JsonPropertyNameAttribute)Attribute.GetCustomAttribute(key, typeof(JsonPropertyNameAttribute));
                if (attr != null&&key.CanRead==true)
                {
                    var value= key.GetValue(this)?.ToString();
                    if (!string.IsNullOrEmpty(value))
                    {
                        keyValuePairs.Add(attr.Name, value);
                    }
                }
            }
            if (keyValuePairs.Count==0)
            {
                return String.Empty;
            }
            var res= keyValuePairs.Select(p => $"{p.Key}={p.Value}");
            return $"?{string.Join("&", res)}";
        }
    }
    public abstract class RequestCore<TRespond>: RequestCore
    {

    }
}
